/*
 * Copyright (c)  2008 Sebastien Tremblay
 *
 * id3-tag-builder - helper.XmlHelper.java
 *
 * All source and documentation is copyrighted by Sebastien Tremblay
 * (seb at ryders dot net) and made available under the Apache License 2.0.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * $Id: XmlHelper.java 30 2010-01-08 04:01:06Z ryders $
 */
package com.empireone.id3tagbuilder.helpers;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * The <code>XmlHelper</code> contains a series of 'toolkit methods' around Xml.
 *
 * @author Sebastien Tremblay
 */
public class XmlHelper {

    /**
     * Finds the node attribute identified by name. Name matched ignoring case.
     * @param node to search attributes in
     * @param name of the attribute to search for
     * @return
     */
    public static Node getAttributeByName(Node node, String name) {
        return getAttributeByName(node, name, true);
    }

    /**
     * Finds the node attribute identified by name.
     * @param node to search attributes in
     * @param name of the attribute to search for
     * @param ignoreCase
     * @return
     */
    public static Node getAttributeByName(Node node, String name, boolean ignoreCase) {

        Node res = null;

        if (node.hasAttributes()) {
            for (int i = 0; i < node.getAttributes().getLength(); i++) {
                if (node.getAttributes().item(i).getNodeName().equals(name) ||
                        (ignoreCase && node.getAttributes().item(i).getNodeName().equalsIgnoreCase(name))) {
                    res = node.getAttributes().item(i);
                }
            }
        }

        return res;
    }

    /**
     * Finds a node in a list based on it's tagName. The tagName will be matched
     * ignoring the case.
     * @param nodes
     * @param name
     * @return node found or null, if unfound.
     */
    public static Node getNodeByName(NodeList nodes, String name) {
        return getNodeByName(nodes, name, true);
    }

    /**
     * Finds a node in a list based on it's tagName.
     * @param nodes
     * @param name
     * @param ignoreCase
     * @return node found or null, if unfound.
     */
    public static Node getNodeByName(NodeList nodes, String name, boolean ignoreCase) {

        Node res = null;

        for (int i = 0; i < nodes.getLength(); i++) {
            if (nodes.item(i).getNodeName().equals(name) ||
                    (ignoreCase && nodes.item(i).getNodeName().equalsIgnoreCase(name))) {
                res = nodes.item(i);
                break;
            }
        }

        return res;

    }
}
